package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ReData;
import com.chixing.ih.json.ReJson;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.dto.FabulousDTO;
import com.chixing.ih.module.programmer.service.IFabulousService;
import com.chixing.ih.util.RedisPraiseUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/4
 * Time: 16:29
 */
@RestController
@RequestMapping("/fabulous")
@Api(tags = "程序员模块点赞相关Api")
public class FabulousController {

    @Autowired
    IFabulousService fabulousService;
    /**
     * desc:点赞接口
     *
     * @param fabulousDTO
     * @return
     */
    @ApiOperation(value = "点赞接口", notes = "点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "likedId", value = "点赞人id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "popularId", value = "被点赞人id", required = true,dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "点赞成功"),
            @ApiResponse(code = 400, message = "点赞失败"),
            @ApiResponse(code = 201, message = "取消点赞"),
            @ApiResponse(code = 401, message = "取消点赞失败")
    })


    @PostMapping("/fabulous")
    public Map<String, Object> saveFabulous(@RequestBody FabulousDTO fabulousDTO) {
        boolean flag = RedisPraiseUtil.redisSet(fabulousDTO.getLikedId(), fabulousDTO.getPopularId());
        long total = RedisPraiseUtil.redisGetTotal(fabulousDTO.getPopularId());
        Programmer programmer = new Programmer();
        programmer.setSupportNum(total);
        programmer.setProgrammerId(fabulousDTO.getPopularId());
        if (flag) {
            boolean flag1 = fabulousService.updateOne(programmer);
            if (flag1)
                return ReJson.returnJson(ReData.SUCCESS1.code, ReData.SUCCESS1.msg);
            return ReJson.returnJson(ReData.FAIL.code, ReData.FAIL.msg);
        } else {
            boolean flag1 = fabulousService.updateOne(programmer);
            if (flag1)
                return ReJson.returnJson(ReData.FAIL_NORMAL7.code, ReData.FAIL_NORMAL7.msg);
            return ReJson.returnJson(ReData.FAIL1.code, ReData.FAIL1.msg);

        }

    }


}
